%% This document created by Scientific Word (R) Version 3.0

\documentclass[a4paper,twoside]{book}
\usepackage[latin1]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage{latexsym}
\usepackage{graphicx}
\usepackage{amsfonts}
\usepackage{amssymb}
%TCIDATA{OutputFilter=latex2.dll}
%TCIDATA{CSTFile=book.cst}
%TCIDATA{LastRevised=Wed Mar 08 15:16:45 2000}
%TCIDATA{<META NAME="GraphicsSave" CONTENT="32">}
\setlength{\oddsidemargin}{0.cm}
\setlength{\evensidemargin}{0.cm}
\setlength{\textwidth}{13.cm}
\setlength{\textheight}{16.cm}
\setlength{\parskip}{0.15cm}
\setlength{\parindent}{0.pt}
\newenvironment{linea}{
\begin{picture}(300,10)
\line(300,0){300}
\end{picture}}{ }
\newenvironment{Ej E/S}
               {\setlength{\parskip}{0.0cm}}
               {\endlist}
\newenvironment{Lista letras}
               {\list{\alph{letra}.}{
                        \usecounter{letra}
                        \itemsep     0.0cm
                        \itemindent   0.0cm
                        \listparindent 0.0cm
                        \itemindent 0.0cm
                        \rightmargin  0.5cm
                        \leftmargin 1.0cm
                        \labelwidth 0.5cm
                        \parsep 0.0cm
                        \topsep 0.0cm
                        \parskip 0.0cm}}
               {\endlist}
\newenvironment{Programa}
               {\ttfamily\small\list{\arabic{conta}.}{\usecounter{conta}
                        \itemsep     0.0cm
                        \itemindent   0.0cm
                        \listparindent 0.0cm
                        \itemindent 0.0cm
                        \rightmargin  0.7cm
                        \leftmargin 1.0cm
                        \labelwidth 0.5cm
                        \parsep 0.0cm
                        \topsep 0.0cm
                        \parskip 0.0cm}}
               {\endlist}
\newenvironment{Programa-cuadro}
               {\begin{lrbox}{\savepar}
                \begin{minipage}[b]{13.cm}
                \ttfamily\list{\arabic{conta}.}{\usecounter{conta}
                        \itemsep     0.0cm
                        \itemindent   0.0cm
                        \listparindent 0.0cm
                        \itemindent 0.0cm
                        \rightmargin  0.7cm
                        \leftmargin 1.0cm
                        \labelwidth 0.5cm
                        \parsep 0.0cm
                        \topsep 0.0cm
                        \parskip 0.0cm}}
               {\endlist\end{minipage}\end{lrbox}\fbox{\usebox{\savepar}}}
\newenvironment{Lista guiones}{
     \begin{list}{-}{
     \setlength{\leftmargin}{0.5cm}
     \setlength{\labelwidth}{0.5cm}
     \setlength{\parsep}{0.pt}
     \setlength{\topsep}{0.0cm}
     \setlength{\parsep}{0.pt}
     \setlength{\itemsep}{0.pt}
     \setlength{\parskip}{0.pt}}
   }
   {\end{list}}
\newsavebox{\savepar}
\newenvironment{recuadro}{
\begin{lrbox}{\savepar}
\begin{minipage}[b]{13.cm}
}
{\end{minipage}\end{lrbox}\fbox{\usebox{\savepar}}}
\newenvironment{Solucion}{\newline \Large {\textbf {SOLUCI\'ON}}}{ }
\newcommand{\enunciado}{\Large{\textbf{ENUNCIADO}}}

\begin{document}
\newcounter{conta} \newcounter{letra} \setcounter{letra}{1}

{\Large {\textbf{ENUNCIADO}} -L4}

Escribir un programa, mediante LEX y YACC, que permita traducir expresiones
aritm\'{e}ticas a notaci\'{o}n polaca inversa. Cada expresi\'{o}n consta de
una sentencia de asignaci\'{o}n. Los nombres permitidos para las variables
constan de una sola letra min\'{u}scula. Cuando en la parte derecha de una
sentencia de asignaci\'{o}n aparece el nombre de una variable a la que
previamente no se le ha asignado ning\'{u}n valor, se deber\'{a} informar y
asignarle el valor 0. El siguiente es un ejemplo de un fichero de entrada:

\begin{Ej E/S}
\qquad\qquad\qquad\qquad b:=27;

\qquad\qquad\qquad\qquad f:=3+5-b;

\qquad\qquad\qquad\qquad x:=2+7*b-f;

\qquad\qquad\qquad\qquad x:=p;
\end{Ej E/S}

La salida que se debe obtener para este fichero es:

\begin{Ej E/S}
\qquad\qquad\qquad\qquad27

\qquad\qquad\qquad\qquad b vale 27

\qquad\qquad\qquad\qquad3 5 + b -

\qquad\qquad\qquad\qquad f vale -19

\qquad\qquad\qquad\qquad2 7 + b * f -

\qquad\qquad\qquad\qquad x vale 210

\qquad\qquad\qquad\qquad p variable no definida

\qquad\qquad\qquad\qquad z vale 0
\end{Ej E/S}

{\Large {\textbf{SOLUCION}} }

Programa LEX

\begin{Programa}
\item \%\%

\item -?\texttt{[1-9]}\texttt{[0-9]}*|0\ \ \ \{\ yylval.valor=atoi(yytext);

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ NUM;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}

\item \texttt{[a-z]}%
\ \ \ \ \ \ \ \ \ \ \ \ \ \{\ yylval.nom=yytext\texttt{[0]};

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ ID;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}

\item \texttt{"}+\texttt{"}\ \ \ \ \ \ \ \ \ \ \{return\ '+';\}

\item \texttt{"}-\texttt{"}\ \ \ \ \ \ \ \ \ \ \{return\ '-';\}

\item \texttt{"}*\texttt{"}\ \ \ \ \ \ \ \ \ \ \{return\ '*';\}

\item \texttt{"}=\texttt{"}\ \ \ \ \ \ \ \ \ \ \{return\ '=';\}

\item \texttt{[\ $\backslash$t]}\ \ \ \ \ \ \ \ \{/*ignorar*/\}

\item $\backslash$n\ \ \ \ \ \ \ \ \ \ \ \{return\ LF;\}

\item .\ \ \ \ \ \ \ \ \ \ \ \ \{printf(\texttt{"}error\ \texttt{"});\}

\item \%\%
\end{Programa}

Programa YACC

\begin{Programa}
\item \%\{

\item  extern\ int\ tab\texttt{[26]};

\item \%\}

\item \%union\{

\item \ \ \ char\ nom;

\item \ \ \ int\ valor;

\item \}

\item \%token\ <valor>\ NUM

\item \%type\ <valor>\ exp

\item \%token\ <nom>\ ID

\item \%token\ LF

\item \%left\ '+'\ '-'\ '*'

\item \%start\ inicio

\item \%\%

\item  inicio\ :\ list\ \ \ \ \ \ \ \ \ \ \ \ \{;\}

\item \ \ \ \ \ \ \ |\ inicio\ LF\ list\ \ \ \{;\}

\item \ \ \ \ \ \ \ ;

\item  list\ \ \ :\ ID\ '='\ exp\ \{

\item \ \ \ \ \ \ \ \ \ \ \ \ \ printf\ ("$\backslash$%
n\ \%c\ toma\ el\ valor\ \%d$\backslash$n",\$1,\$3);

\item \ \ \ \ \ \ \ \ \ \ \ \ \ tab\texttt{[\$1-'a']}\ =\ \$3;

\item \ \ \ \ \ \ \ \ \ \}

\item \ \ \ \ \ ;

\item  exp\ \ :\ NUM\ \{\ printf\ ("\%d",\$1);\ \$\$\ =\ \$1;\ \}

\item \ \ \ \ \ |\ exp\ '+'\ exp\ \{\ printf\ ("+");\ \$\$\ =\ \$1+\$3;\ \}

\item \ \ \ \ \ |\ exp\ '*'\ exp\ \{\ printf\ ("*");\ \$\$\ =\ \$1*\$3;\}

\item \ \ \ \ \ |\ exp\ '-'\ exp\ \{printf\ ("-");\ \$\$\ =\ \$1-\$3;\}

\item \ \ \ \ \ |\ ID\ \{

\item \ \ \ \ \ \ \ \ \ \ \ if\ (tab\texttt{[\$1-'a']}==-99)\ \{

\item
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ printf\ ("la\ variable\ \%c\ no\ esta\ def$\backslash
$n",\$1);

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ tab\texttt{[\$1-'a']}=0;

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \$\$=0;

\item \ \ \ \ \ \ \ \ \ \ \ \}\ else

\item \ \ \ \ \ \ \ \ \ \ \ \ \ \ \$\$=tab\texttt{[\$1-'a']};

\item \ \ \ \ \ \ \ \ \ \ \ printf("\%c",\$1);

\item \ \ \ \ \ \ \ \}

\item \ \ \ \ \ ;

\item \%\%
\end{Programa}
\end{document}